function [resultFinal, Ir ] = characterSegmentation( img, IS_DEBUG, FOLDER_RESULT,file_name, mode )
%CHARACTER_SEGMENTATION Summary of this function goes here
%   Detailed explanation goes here
% H = fspecial('gaussian' ,[5 5],2); % create unsharp mask
%         img = imfilter(img,H);
% SE = strel('disk',5);
% img = imerode(img, SE);
% imwrite(img,strcat(FOLDER_RESULT,'\',file_name,'__.bmp'),'bmp');
switch(mode)
    case 0
        [result,Ir]= characterSegment.character_Segmentation0(img, IS_DEBUG, FOLDER_RESULT,file_name);
    case 1
        %         imwrite(img,strcat(FOLDER_RESULT,'\',file_name,'_0.bmp'),'bmp');
        [result,Ir]= characterSegment.character_Segmentation123(img, IS_DEBUG, FOLDER_RESULT,file_name, mode);
    case 2
        [result,Ir]= characterSegment.character_Segmentation123(img, IS_DEBUG, FOLDER_RESULT,file_name, mode);
    case 3
        [result,Ir]= characterSegment.character_Segmentation123(img, IS_DEBUG, FOLDER_RESULT,file_name, mode);
    case 4
        [result,Ir]= characterSegment.character_Segmentation4(img, IS_DEBUG, FOLDER_RESULT,file_name);
end

highO = zeros(1,length(result));
widthO = zeros(1,length(result));
for i=1:length(result)
    widthO(1,i) = size(result{i},2);
end
meanHigh = mean(highO);
meanWidth = mean(widthO);
num = 0;
resultFinal = {};
for i=1:length(result)
    if (highO(1,i) > (meanHigh -10)) && (highO(1,i) < (meanHigh +10))
        temp1 = result{i};
        H = fspecial('gaussian' ,[5 5],2); % create unsharp mask
        temp1 = imfilter(temp1,H);
        
        w = size(temp1,2);
        if(w<meanWidth-10)
            temp1= imresize(temp1, [64 NaN]);
            p1=floor((64-w)/2);
            if p1>0
                p2=64-w-p1;
                temp1 = padarray(temp1, [0 p1], 0, 'pre');
                if p2>0
                    temp1 = padarray(temp1, [0 p2], 0, 'post');
                end
            end
        end
        tempim = imresize(temp1,[64 64]);
        if mode == 4
            SE = ones(1,3);
            tempim = imopen(tempim,SE);
            SE = ones(3,1);
            tempim = imopen(tempim,SE);
            SE = strel('disk',2);
            tempim = imclose(tempim,SE);
        end
        msk=[0 0 0 0 ;
            0 1 1 0 ;
            0 1 1 0 ;
            0 0 0 0 ];
        tempim=conv2(double(tempim),double(msk));
        num = num +1;
        tempimf = logical(tempim);
        tempimf1 = bwareaopen(~tempimf, 60);
        resultFinal{num} = tempimf|(~tempimf1);
    end
end

end